﻿using FocusClock.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using static System.Windows.Forms.AxHost;

namespace FocusClock.WPF
{
    public class ViewModel : INotifyPropertyChanged
    {
        private readonly ClockManager clockManager = null;
        private bool started = false;
        public ViewModel()
        {
            FoucsTime = 30;
            RestTime = 5;
            this.clockManager = new ClockManager();


            clockManager.onRunning += ClockManager_onRunning;
            clockManager.onStart += ClockManager_onStart;
            clockManager.onReset += ClockManager_onReset;

            Start = new DefaultCommand(sender => true, sender =>
            {
                started = true;
                clockManager.RestTime = RestTime * 60;
                clockManager.FoucsTime = FoucsTime * 60;
                clockManager.Start();
            });

            Reset = new DefaultCommand(sender => true, sender =>
            {
                clockManager.Rest();
            });

            PauseOrContinue = new DefaultCommand(sender => true, sender =>
            {
                if (clockManager.IsRunning && clockManager.IsTimeout)
                {
                    if (clockManager.ActionType == ClockActionType.Focus)
                    {
                        clockManager.StopFoucs();
                    }
                    else if (clockManager.ActionType == ClockActionType.Rest)
                    {
                        clockManager.StopRest();
                    }
                    ClockManager_onRunning();
                }
                else
                {
                    clockManager.PauseOrContinue();
                }
            });
        }

        private void ClockManager_onReset()
        {
            OnPropertyChanged("IsShowRunningPanel");
            ClockManager_onRunning();
        }

        private void ClockManager_onStart()
        {
            OnPropertyChanged("IsShowRunningPanel");
            ClockManager_onRunning();
        }

        private void ClockManager_onRunning()
        {
            OnPropertyChanged("StatusContent");
            OnPropertyChanged("Minutes");
            OnPropertyChanged("Seconds");
            OnPropertyChanged("IsShowSecond");
            OnPropertyChanged("IsShowReset");
            OnPropertyChanged("PauseOrContinueText");
            OnPropertyChanged("RunningPanelBG");
            OnPropertyChanged("RunningTextColor");
        }

        /// <summary>
        /// 专注时间  秒
        /// </summary>
        public int FoucsTime { get; set; }
        /// <summary>
        /// 休息时间  秒
        /// </summary>
        public int RestTime { get; set; }

        public string StatusContent
        {
            get
            {
                if (clockManager.ActionType == ClockActionType.Focus)
                {
                    return $"专注时段{(clockManager.IsTimeout?"超时":"")}";
                }
                else if (clockManager.ActionType == ClockActionType.Rest)
                {
                    return $"休息时段{(clockManager.IsTimeout ? "超时" : "")}";
                }
                return string.Empty;
            }
        }

        public string Minutes
        {
            get
            {
                var minutes = Math.Abs(clockManager.Countdown) / 60;
                if (minutes > 0)
                {
                    if (minutes > 99)
                    {
                        minutes = 99;
                    }
                    return $"{(minutes < 10 ? "0" : "")}{minutes}";
                }
                else
                {
                    return Seconds;
                }

            }
        }

        public string Seconds
        {
            get
            {
                var seconds = Math.Abs(clockManager.Countdown) % 60;
                return $"{(seconds < 10 ? "0" : "")}{seconds}";
            }
        }

        public string PauseOrContinueText
        {
            get
            {
                if(clockManager.IsTimeout)
                {
                    return clockManager.IsRunning ? "▶" : "▶";
                }
                return clockManager.IsRunning ? "▣" : "▶";
            }
        }

        public string RunningPanelBG
        {
            get
            {
                if(clockManager.IsTimeout) {
                    if(clockManager.Countdown %2 == 0)
                    {
                        return "#CC3333";
                    }
                    return "#003366";
                }
                else
                {
                    return "#FFFFFFFF";
                }
            }
        }


        public string RunningTextColor
        {
            get
            {
                if (clockManager.IsTimeout)
                {
                    return "#FFFFFF";
                }
                else
                {
                    return "#606060";
                }
            }
        }

        private object _obj;
        public Visibility IsShowSecond { get => Math.Abs(clockManager.Countdown) / 60 > 0 ? Visibility.Visible : Visibility.Hidden; set { _obj = value; } }
        public Visibility IsShowRunningPanel { get => clockManager.IsRunning ? Visibility.Visible : Visibility.Hidden; set { _obj = value; } }
        public Visibility IsShowReset { get => started&&clockManager.IsRunning ? Visibility.Hidden : Visibility.Visible; set { _obj = value; } }

        public event PropertyChangedEventHandler? PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public ICommand Start { get; protected set; }
        public ICommand Reset { get; protected set; }
        public ICommand PauseOrContinue { get; protected set; }
    }
}
